<template>
  <el-dialog
    :title="!dataForm.catIdentify ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="猫的编号" prop="catAreaId">
      <el-input v-model="dataForm.catAreaId" placeholder="猫的编号"></el-input>
    </el-form-item>
    <el-form-item label="区域地址" prop="catAreaAddress">
      <el-input v-model="dataForm.catAreaAddress" placeholder="区域地址"></el-input>
    </el-form-item>
    <el-form-item label="房间号码" prop="catAreaRoom">
      <el-input v-model="dataForm.catAreaRoom" placeholder="房间号码"></el-input>
    </el-form-item>
    <el-form-item label="移动时间" prop="catAreaDate">
      <el-input v-model="dataForm.catAreaDate" placeholder="移动时间"></el-input>
    </el-form-item>
    <el-form-item label="照顾人员" prop="catAreaCarer">
      <el-input v-model="dataForm.catAreaCarer" placeholder="照顾人员"></el-input>
    </el-form-item>
    <el-form-item label="区域描述" prop="catAreaDesc">
      <el-input v-model="dataForm.catAreaDesc" placeholder="区域描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          catIdentify: 0,
          catAreaId: '',
          catAreaAddress: '',
          catAreaRoom: '',
          catAreaDate: '',
          catAreaCarer: '',
          catAreaDesc: ''
        },
        dataRule: {
          catAreaId: [
            { required: true, message: '猫的编号不能为空', trigger: 'blur' }
          ],
          catAreaAddress: [
            { required: true, message: '区域地址不能为空', trigger: 'blur' }
          ],
          catAreaRoom: [
            { required: true, message: '房间号码不能为空', trigger: 'blur' }
          ],
          catAreaDate: [
            { required: true, message: '移动时间不能为空', trigger: 'blur' }
          ],
          catAreaCarer: [
            { required: true, message: '照顾人员不能为空', trigger: 'blur' }
          ],
          catAreaDesc: [
            { required: true, message: '区域描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.catIdentify = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.catIdentify) {
            this.$http({
              url: this.$http.adornUrl(`/cat/arearecord/info/${this.dataForm.catIdentify}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.catAreaId = data.areaRecord.catAreaId
                this.dataForm.catAreaAddress = data.areaRecord.catAreaAddress
                this.dataForm.catAreaRoom = data.areaRecord.catAreaRoom
                this.dataForm.catAreaDate = data.areaRecord.catAreaDate
                this.dataForm.catAreaCarer = data.areaRecord.catAreaCarer
                this.dataForm.catAreaDesc = data.areaRecord.catAreaDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cat/arearecord/${!this.dataForm.catIdentify ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'catIdentify': this.dataForm.catIdentify || undefined,
                'catAreaId': this.dataForm.catAreaId,
                'catAreaAddress': this.dataForm.catAreaAddress,
                'catAreaRoom': this.dataForm.catAreaRoom,
                'catAreaDate': this.dataForm.catAreaDate,
                'catAreaCarer': this.dataForm.catAreaCarer,
                'catAreaDesc': this.dataForm.catAreaDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
